#include <retro.h>
#include "retro.creator.h"

using namespace Retro::Creator::Dialog;
using namespace SPLib::Windows;

TCHAR gLangName[][20] = {
  _T("Lua")
};

Data::ScriptLanguage gLangVal[] = {
  Data::ScriptLua
};

VOID    EditScript::onUnload()
{
}

VOID    EditScript::onLoad()
{
  addEdit(mEditName, mLblName, _T("Name:"));
  addSeparator(mSepName);

  addComboBox(mCombLang, mLblLang, _T("Language:"));
  ComboBox_AddStrings(mCombLang, gLangName);

  addOKCancel(mBtnOK, mBtnCancel, mSepButtons);
}

VOID    EditScript::onCommand(Event::CommandEvent& iEvent)
{
}

VOID    EditScript::onNotify(Event::NotifyEvent& iEvent)
{
  switch (iEvent.data->idFrom)
  {
  case IDOK:
    if (GetWindowTextLength(mEditName) > 0)
    {
      setResult(DialogOK);
    }
    else
    {
      MessageBox(*this, _T("Name cannot be empty"), NULL, MB_ICONERROR);
    }
  case IDCANCEL:
    hide();
    break;
  }
}

VOID    EditScript::newScript()
{
  TCHAR szName[RETRO_MAXTEXT];

  _stprintf(szName, _T("Script %.4d"),
    Frame::get()->getProject().getNextResourceID(Data::ResourceScript));

  ComboBox_SetCurSel(mCombLang, 0);
  Edit_SetText(mEditName, szName);

  setText(_T("New Script"));
}

VOID    EditScript::setScript(Data::Script* value)
{
  INT wLangIndex = 0;

  setText(_T("Script Properties"));

  Array_ForEachI(i, gLangVal)
  {
    if (value->getLanguage() == gLangVal[i])
    {
      wLangIndex = i;
      break;
    }
  }

  ComboBox_SetCurSel(mCombLang, wLangIndex);
  Edit_SetText(mEditName, value->getName());
}

VOID    EditScript::getScript(Data::Script* value)
{
  TCHAR szName[RETRO_MAXNAME];

  Edit_GetText(mEditName, szName, RETRO_MAXNAME);

  value->setName(szName);
  value->setLanguage(gLangVal[ComboBox_GetCurSel(mCombLang)]);
}
